package com.green.service.manage.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.green.common.Constant;
import com.green.common.ContextHolder;
import com.green.common.page.Pagination;
import com.green.dao.manage.OrgDao;
import com.green.dao.manage.PersonDao;
import com.green.dao.manage.PositionDao;
import com.green.dao.manage.SyncErpDataDao;
import com.green.model.manage.Org;
import com.green.model.manage.Person;
import com.green.model.manage.Position;
import com.green.model.manage.enums.BizTypeEnum;
import com.green.model.manage.enums.OrgEnum;
import com.green.service.common.impl.BaseServiceImpl;
import com.green.service.manage.SyncErpDataService;

@Component("syncErpDataService")
public class SyncErpDataServiceImpl extends BaseServiceImpl implements SyncErpDataService {
	@Resource
	private SyncErpDataDao syncErpDataDao;
	@Resource
	private OrgDao orgDao;
	@Resource
	private PositionDao positionDao;
	@Resource
	private PersonDao personDao;

	@Override
	public void syncErpDatas(String orgNumber,String orgId) {
		final int PAGE_SIZE = 5000;
		/*// 切换到ERP数据源*/
		ContextHolder.setDataSource("erpDataSource");
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("orgNumber", orgNumber);
		// ERP组织
		Pagination<Org> orgList = syncErpDataDao.getErpOrgs(new Pagination<Org>(PAGE_SIZE, 1), params);
		// ERP职务
		Pagination<Position> positionList = syncErpDataDao.getErpPositions(new Pagination<Position>(PAGE_SIZE, 1), params);
		// ERP人员
		Pagination<Person> personList = syncErpDataDao.getErpPersons(new Pagination<Person>(PAGE_SIZE, 1), params);
         /*
		// 切换到green数据源*/
		ContextHolder.setDataSource("greenOfficialData");
		params.clear();
		
		params.put("controUnit", orgId);
		// green已经存在的数据
		List<Org> oldOrgList = orgDao.getOrgList(params);
		List<Position> oldPositionList = positionDao.getPositionList(params);
		List<Person> oldPersonList = personDao.getPersonList(params);
		// list转成Map
		Map<String, Object> oldOrgMap = new HashMap<String, Object>();
		for (Org org : oldOrgList) {
			oldOrgMap.put(org.getId(), org);
		}
		Map<String, Object> oldPositionMap = new HashMap<String, Object>();
		for (Position position : oldPositionList) {
			oldPositionMap.put(position.getId(), position);
		}
		Map<String, Object> oldPersonMap = new HashMap<String, Object>();
		for (Person person : oldPersonList) {
			oldPersonMap.put(person.getId(), person);
		}
		Map<String, Object> erpPersonMap = new HashMap<String, Object>();
		for (Person person : personList.getItems()) {
			erpPersonMap.put(person.getId(), person);
		}
		Org controlUnit = null;
		// 同步组织
		for (Org org : orgList.getItems()) {
			if (orgNumber.equals(org.getNumber())) {
				controlUnit = org;
				org.setOrgType(OrgEnum.BLOC);
				org.setParent(new Org());
			} else {
				if (org.getBizType() != null && org.getBizType().contains(BizTypeEnum.CITYUNIT.toString())) {
					org.setOrgType(OrgEnum.COMPANY);
				} else if (org.getBizType() != null && org.getBizType().contains(BizTypeEnum.INTERMEDIARY.toString())) {
					org.setOrgType(OrgEnum.REGION);
				} else {
					org.setOrgType(OrgEnum.FUNCTIONS);
				}
			}
			org.setControUnit(controlUnit);
			
			// green已经存在的组织做更新处理，没有的做新增
			if (oldOrgMap.get(org.getId()) != null) {
				orgDao.updateOrg(org);
			} else {
				orgDao.insertOrg(org);
			}
		}
		// 同步职务
		for (Position position : positionList.getItems()) {
			position.setControUnit(controlUnit);
			// green已经存在的职务做更新处理，没有的做新增
			if (oldPositionMap.get(position.getId()) != null) {
				positionDao.updatePosition(position);
			} else {
				positionDao.insertPosition(position);
			}
		}
		// 同步人员
		for (Person person : personList.getItems()) {
			person.setControUnit(controlUnit);
			// green已经存在的人员做更新处理，没有的做新增
			if (oldPersonMap.get(person.getId()) != null) {
				personDao.updatePerson(person);
			} else {
				personDao.insertPerson(person);
			}
		}

		// 检查green当前人员 是否存在于最新的ERP人员列表里
		for (Person old : oldPersonList) {
			// 过滤ADMIN账号和运营yy帐号
			if (Constant.ADMIN_NUMBER.contains(old.getNumber()) || Constant.YUNYING_NUMBER.equals(old.getNumber())) {
				continue;
			}
			// 如果green当前人员不存在于最新的ERP人员列表里 将其禁用
			if (erpPersonMap.get(old.getId()) == null) {
				old.setControUnit(controlUnit);
				old.setIsForbidden(1);
				personDao.updatePerson(old);
			}
		}
	}
}
